package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.mapper.LeaveMapper;
import com.ruoyi.system.domain.Leave;
import com.ruoyi.system.service.ILeaveService;
import com.ruoyi.system.service.IAuditNodeService;
import com.ruoyi.system.service.IAuditRouteService;
import com.ruoyi.system.service.IAuditAssigneeService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.common.utils.StringUtils;

/**
 * 请假Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-11-06
 */
@Service
public class LeaveServiceImpl implements ILeaveService 
{
    @Autowired
    private LeaveMapper leaveMapper;

    @Autowired
    private IAuditNodeService auditNodeService;

    @Autowired
    private IAuditRouteService auditRouteService;

    @Autowired
    private IAuditAssigneeService auditAssigneeService;

    @Autowired
    private ISysUserService userService;


    /**
     * 查询请假
     * 
     * @param leaveId 请假主键
     * @return 请假
     */
    @Override
    public Leave selectLeaveByLeaveId(Long leaveId)
    {
        Leave leave = leaveMapper.selectLeaveByLeaveId(leaveId);
        leave.setAuditInfo(auditRouteService.selectAuditRoutesByBusinessTypeAndBusinessId(leave.getAuditBusinessType(), leave.getLeaveId()));
        return leave;
    }

    /**
     * 查询请假列表
     * 
     * @param leave 请假
     * @return 请假
     */
    @Override
    public List<Leave> selectLeaveList(Leave leave)
    {
        return leaveMapper.selectLeaveList(leave);
    }

    /**
     * 新增请假
     * 
     * @param leave 请假
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertLeave(Leave leave)
    {
        leave.setCreateTime(DateUtils.getNowDate());
        int result = leaveMapper.insertLeave(leave);
        
        // 新增请假后，根据auditBusinessType查询audit_node，插入audit_route和audit_assignee
        if (result > 0 && StringUtils.isNotEmpty(leave.getAuditBusinessType())) {
            Long firstAuditNodeId = auditRouteService.initAuditRoute(leave.getAuditBusinessType(), leave.getLeaveId());
            leave.setCurrentAuditRouteId(firstAuditNodeId);
            this.updateLeave(leave);
        }
        
        return result;
    }



    /**
     * 修改请假
     * 
     * @param leave 请假
     * @return 结果
     */
    @Override
    public int updateLeave(Leave leave)
    {
        leave.setUpdateTime(DateUtils.getNowDate());
        return leaveMapper.updateLeave(leave);
    }

    /**
     * 批量删除请假
     * 
     * @param leaveIds 需要删除的请假主键
     * @return 结果
     */
    @Override
    public int deleteLeaveByLeaveIds(Long[] leaveIds)
    {
        return leaveMapper.deleteLeaveByLeaveIds(leaveIds);
    }

    /**
     * 删除请假信息
     * 
     * @param leaveId 请假主键
     * @return 结果
     */
    @Override
    public int deleteLeaveByLeaveId(Long leaveId)
    {
        return leaveMapper.deleteLeaveByLeaveId(leaveId);
    }
}